package cn.kimming.system.controller;

import cn.kimming.common.dto.CheckInDTO;
import cn.kimming.common.dto.CheckOutDTO;
import cn.kimming.common.dto.ReserveDTO;
import cn.kimming.common.dto.ReserveItemDTO;
import cn.kimming.common.pojo.Orderbill;
import cn.kimming.common.pojo.OrderbillItem;
import cn.kimming.common.pojo.Room;
import cn.kimming.common.util.Result;
import cn.kimming.common.vo.CheckInVO;
import cn.kimming.common.vo.CheckOutVO;
import cn.kimming.common.vo.OrderbillVO;
import cn.kimming.service.IOrderbillService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author 刘铭轩
 * date 2021-06-09
 */

@RestController
@RequestMapping("/orderbill")
public class OrderbillController {
    @Autowired
    private IOrderbillService orderbillService;
    @PostMapping("/reserve")
    public Result reserve(@Valid @RequestBody ReserveDTO dto) {
        List<OrderbillItem> items = new ArrayList<>();
        for (ReserveItemDTO itemDto : dto.getOrderbillItems()) {
            OrderbillItem itemBo = new OrderbillItem();
            BeanUtils.copyProperties(itemDto, itemBo);
            items.add(itemBo);
        }
        Orderbill bo = new Orderbill();
        BeanUtils.copyProperties(dto, bo, "orderbillItems");
        bo.setOrderbillItems(items);
        orderbillService.reserve(bo);
        return new Result(true, "预定成功");
    }

    @GetMapping("/info/{page}")
    public Result findInfo(@PathVariable int page, String phone) {
        PageInfo<OrderbillVO> info = orderbillService.findInfo(page, phone);
        return new Result(true, "查询成功").addData("info", info);
    }


    @GetMapping("/getorderid/{phone}")
    public Result getOrderIdByPhone(@PathVariable String phone) {
        String orderId = orderbillService.getOrderIdByPhone(phone);
        return new Result(true, "查询成功").addData("orderId", orderId);
    }
    @GetMapping("/checkin/{orderId}")
    public Result findCheckInVOByOrderId(@PathVariable String orderId) {
        CheckInVO info = orderbillService.findCheckInVOByOrderId(orderId);
        return new Result(true, "查询成功").addData("info", info);
    }

    @PostMapping("/checkin")
    public Result checkIn(@Valid @RequestBody CheckInDTO dto) {
        Set<Integer> roomNums = orderbillService.checkIn(dto);
        return new Result(true, "查询成功").addData("roomNums", roomNums);
    }

    @GetMapping("/checkout/{roomNum}")
    public Result findCheckOutVOByRoomNum(@PathVariable Integer roomNum) {
        CheckOutVO info = orderbillService.findCheckOutVOByRoomNum(roomNum);
        return new Result(true, "查询成功").addData("info", info);
    }

    @PostMapping("/checkout")
    public Result checkout(@RequestBody @Valid CheckOutDTO dto) {
        BigDecimal totalPrice = orderbillService.checkout(dto);
        return new Result(true, "退房结账成功").addData("totalPrice", totalPrice);
    }
}